package com.ichina.chat.action;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.util.ServletContextAware;

import com.ichina.chat.util.StringReplacer;
import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class MessageAction extends ActionSupport implements
		ServletRequestAware, ServletContextAware {

	private HttpServletRequest request;
	private ServletContext servletContext;
	private String result = "error";
	private String mess;
	private String replyMessage;
	private String uname;
	private String yname;
	private String color;

	@SuppressWarnings("unchecked")
	public String show() {
		StringBuffer str = new StringBuffer();
		SimpleDateFormat d = new SimpleDateFormat("yyyy-dd-MM hh:mm:ss");
		Map<String, String> map = (Map<String, String>) servletContext
				.getAttribute("map");
		try {
			if (mess != null) {
				mess = java.net.URLDecoder.decode(mess, "UTF-8");
			}
			if (replyMessage != null) {
				replyMessage = java.net.URLDecoder
						.decode(replyMessage, "UTF-8");
			}
			if (uname != null) {
				uname = java.net.URLDecoder.decode(uname, "UTF-8");
			}
			if (yname != null && !"".equals(yname)) {
				yname = java.net.URLDecoder.decode(yname, "UTF-8");
			}
			if (color != null) {
				color = java.net.URLDecoder.decode(color, "UTF-8");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (map == null) {
			map = new HashMap<String, String>();
		}
		// 判断用户是否已经存在map中
		if (map.containsKey(uname)) {
			// 判断该用户下是否有数据
			if (map.get(uname) != null) {
				str.append(map.get(uname));
			}
		}
		if (mess != null) {
			mess = StringReplacer.replaceString(mess, new String[] { ">", "<",
					"我操", "我草", "去死", "你妈", "垃圾", "系统信息", "|", "$", "#" },
					new String[] { "&gt;", "&lt;", "**", "**", "**", "**",
							"**", "****", "｜", "￥", "＃" });
			if (mess.length() > 2) {
				// 替换聊天信息中的图片代号
				int x = 0, y = 0;
				for (int i = 0; i <= 134; i++) {
					if (i < 15) {
						x = 480 - i * 32;
						y = -1;
					} else if (i > 14 && i < 30) {
						x = 480 - (i - 15) * 32;
						y = 234 - 18;
					} else if (i > 29 && i < 45) {
						x = 480 - (i - 15 * 3) * 32;
						y = 234 - 22 * 2 - 1;
					} else if (i > 44 && i < 60) {
						x = 480 - (i - 15 * 4) * 32;
						y = 234 - 24 * 3;
					} else if (i > 59 && i < 75) {
						x = 480 - (i - 15 * 5) * 32;
						y = 234 - 24 * 4 - 3;
					} else if (i > 74 && i < 90) {
						x = 480 - (i - 15 * 6) * 32;
						y = 234 - 25 * 5 - 1;
					} else if (i > 89 && i < 105) {
						x = 480 - (i - 15 * 7) * 32;
						y = 234 - 25 * 6 - 3;
					} else if (i > 104 && i < 120) {
						x = 480 - (i - 15 * 8) * 32;
						y = 234 - 25 * 7 - 5;
					} else if (i > 119 && i < 135) {
						x = 480 - (i - 15 * 9) * 32 - 1;
						y = -217;
					}
					String div = "<button style='background:url(/chat/j/images/face.gif) "
							+ x
							+ "px "
							+ y
							+ "px;width: 30px;height: 26px;border: 0px;margin: 0px 0px 0px 0px;padding: 0px 0px 0px 0px;'>&nbsp;</button>";
					mess = StringReplacer.replaceString(mess, "[F" + i + "]",
							div);
				}
			}
			str.append("<font color='#009933' face='微软雅黑' size='2' >" + uname
					+ ":&nbsp;");
			str.append(d.format(new Date()) + "</font><br/>");
			str.append("<font color='#" + color + "' face='微软雅黑' size='2'>"
					+ mess + "</font><br/>");

		} else if (replyMessage != null) {
			replyMessage = StringReplacer.replaceString(replyMessage,
					new String[] { ">", "<", "我操", "我草", "去死", "你妈", "垃圾",
							"系统信息", "|", "$", "#" }, new String[] { "&gt;",
							"&lt;", "**", "**", "**", "**", "**", "****", "｜",
							"￥", "＃" });
			if (replyMessage.length() > 2) {
				// 替换聊天信息中的图片代号
				int x = 0, y = 0;
				for (int i = 0; i <= 134; i++) {
					if (i < 15) {
						x = 480 - i * 32;
						y = -1;
					} else if (i > 14 && i < 30) {
						x = 480 - (i - 15) * 32;
						y = 234 - 18;
					} else if (i > 29 && i < 45) {
						x = 480 - (i - 15 * 3) * 32;
						y = 234 - 22 * 2 - 1;
					} else if (i > 44 && i < 60) {
						x = 480 - (i - 15 * 4) * 32;
						y = 234 - 24 * 3;
					} else if (i > 59 && i < 75) {
						x = 480 - (i - 15 * 5) * 32;
						y = 234 - 24 * 4 - 3;
					} else if (i > 74 && i < 90) {
						x = 480 - (i - 15 * 6) * 32;
						y = 234 - 25 * 5 - 1;
					} else if (i > 89 && i < 105) {
						x = 480 - (i - 15 * 7) * 32;
						y = 234 - 25 * 6 - 3;
					} else if (i > 104 && i < 120) {
						x = 480 - (i - 15 * 8) * 32;
						y = 234 - 25 * 7 - 5;
					} else if (i > 119 && i < 135) {
						x = 480 - (i - 15 * 9) * 32 - 1;
						y = -217;
					}
					String div = "<button style='background:url(/chat/j/images/face.gif) "
						+ x
						+ "px "
						+ y
						+ "px;width: 30px;height: 26px;border: 0px;margin: 0px 0px 0px 0px;padding: 0px 0px 0px 0px;'>&nbsp;</button>";
					replyMessage = StringReplacer.replaceString(replyMessage,
							"[F" + i + "]", div);
				}
			}
			str.append("<font color='red' face='微软雅黑' size='2' >客服:&nbsp;");
			str.append(d.format(new Date()) + "</font><br/>");
			str.append("<font color='#" + color + "' face='微软雅黑' size='2'>"
					+ replyMessage + "</font><br/>");
		}
		if (uname != null) {
			request.setAttribute("user", uname);
		}
		map.put(uname, str.toString());
		// 将聊天信息添加到容器中\
		servletContext.setAttribute("map", map);
		return "message";
	}

	@SuppressWarnings("unchecked")
	public String reqUser() {
		if (uname != null) {
			try {
				uname = java.net.URLDecoder.decode(uname, "UTF-8");
			} catch (Exception e) {
				e.printStackTrace();
			}
			request.setAttribute("user", uname);
			List<String> userList = (List<String>) servletContext
					.getAttribute("userList");
			if (userList == null) {
				userList = new ArrayList<String>();
			}
			try {
				boolean flag = true;
				for (String s : userList) {
					if (uname.equals(s)) {
						flag = false;
					}
				}
				if (flag) {
					userList.add(uname);
				}

			} catch (Exception e) {
				e.printStackTrace();
			}
			servletContext.setAttribute("userList", userList);

		}
		result = "message";
		return result;
	}

	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}

	public void setServletContext(ServletContext servletContext) {
		this.servletContext = servletContext;
	}

	public String getMess() {
		return mess;
	}

	public void setMess(String mess) {
		this.mess = mess;
	}

	public String getUname() {
		return uname;
	}

	public void setUname(String uname) {
		this.uname = uname;
	}

	public String getYname() {
		return yname;
	}

	public void setYname(String yname) {
		this.yname = yname;
	}

	public String getColor() {
		return color;
	}

	public void setColor(String color) {
		this.color = color;
	}

	public String getReplyMessage() {
		return replyMessage;
	}

	public void setReplyMessage(String replyMessage) {
		this.replyMessage = replyMessage;
	}
}
